﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;
using DCSLibrary;

public partial class Admin_Form_CategoryPage_Add : System.Web.UI.Page
{
    #region Cac bien dung chung
    FunctionPageUI objFunctionPage = new FunctionPageUI();
    CategoryPageUI objCategoryPage = new CategoryPageUI();
    DataSet dsFunctionPage;
    DataSet dsCategoryPage;

    int productPage = 4;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        dsCategoryPage = objCategoryPage.GetAll();
        dsFunctionPage = objFunctionPage.GetAll();
        if (!IsPostBack)
        {
            BindFunctionPage();
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }


    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        string alias = Utils.Format.ConvertToNoSign(txtCategoryPageName.Text.Trim());

        //Gen url path cua trang moi tao
        string urlPath = General.GenUrlPath((cboCategoryPage.SelectedValue.Trim() == "" ? productPage : Convert.ToInt32(cboCategoryPage.SelectedValue)), alias, dsFunctionPage);

        if (txtCategoryPageID.Value.Trim() == "")
        {
            try
            {
                int result = objCategoryPage.Add(txtCategoryPageName.Text, alias, txtCategoryPageTitle.Text, txtCategoryPageImage.Text, txtCategoryPageIcon.Text,
                    txtMetaKeyword.Text, txtMetaDescription.Text, chkIsVisible.Checked, chkIsAllowDelete.Checked, Convert.ToInt32(txtViewOrder.Text),
                    chkIsCache.Checked, Convert.ToInt32(txtCacheTime.Text), rblControlPath.SelectedValue == "1"?true:false, urlPath,
                    txtCategoryPageAbstract.Text, txtCategoryPageContent.Content, (cboCategoryPage.SelectedValue.Trim() == "" ? productPage : Convert.ToInt32(cboCategoryPage.SelectedValue)), chkIsNew.Checked, chkIsTypical.Checked, chkIsHot.Checked);

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objCategoryPage.Update(Convert.ToInt32(txtCategoryPageID.Value), txtCategoryPageName.Text, alias, txtCategoryPageTitle.Text, txtCategoryPageImage.Text, txtCategoryPageIcon.Text,
                    txtMetaKeyword.Text, txtMetaDescription.Text, chkIsVisible.Checked, chkIsAllowDelete.Checked, Convert.ToInt32(txtViewOrder.Text),
                    chkIsCache.Checked, Convert.ToInt32(txtCacheTime.Text), rblControlPath.SelectedValue == "1" ? true : false, urlPath,
                    txtCategoryPageAbstract.Text, txtCategoryPageContent.Content, (cboCategoryPage.SelectedValue.Trim() == "" ? productPage : Convert.ToInt32(cboCategoryPage.SelectedValue)), chkIsNew.Checked, chkIsTypical.Checked, chkIsHot.Checked);

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-chung/quan-tri-trang-nhom.aspx");
    }

    private void BindFunctionPage()
    {
        cboCategoryPage.DataSource = dsFunctionPage.Tables[0];
        cboCategoryPage.DataBind();
    }

    private void BindCategoryPage(int FunctionPageID)
    {
        DataTable dt = TableLib.CreateTable(dsCategoryPage.Tables[0], "FunctionPageID = " + FunctionPageID);
        cboCategoryPage.DataSource = dt;
        cboCategoryPage.DataBind();
    }

    private void LoadEditData(int CategoryPageID)
    {
        DataSet ds = objCategoryPage.GetByID(CategoryPageID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtCategoryPageID.Value = row["CategoryPageID"].ToString();
            txtCacheTime.Text = row["CacheTime"].ToString();
            txtCategoryPageAbstract.Text = row["CategoryPageAbstract"].ToString();
            txtCategoryPageContent.Content = row["CategoryPageContent"].ToString();
            txtCategoryPageIcon.Text = row["FunctionPageIcon"].ToString();
            txtCategoryPageImage.Text = row["FunctionPageImage"].ToString();
            txtCategoryPageName.Text = row["FunctionPageName"].ToString();
            txtCategoryPageTitle.Text = row["FunctionPageTitle"].ToString();
            txtMetaDescription.Text = row["MetaDescription"].ToString();
            txtMetaKeyword.Text = row["MetaKeyword"].ToString();
            txtViewOrder.Text = row["ViewOrder"].ToString();
            chkIsAllowDelete.Checked = Convert.ToBoolean(row["AllowDelete"].ToString());
            chkIsCache.Checked = Convert.ToBoolean(row["IsCache"].ToString());
            chkIsHot.Checked = Convert.ToBoolean(row["IsHot"].ToString());
            chkIsNew.Checked = Convert.ToBoolean(row["IsNew"].ToString());
            chkIsTypical.Checked = Convert.ToBoolean(row["IsTypical"].ToString());
            chkIsVisible.Checked = Convert.ToBoolean(row["IsVisible"].ToString());
            rblControlPath.SelectedIndex = 0;
            General.SetSelectedItemInComboBox(row["ParentID"].ToString(), cboCategoryPage);
            btnCreate.Text = "Cập nhật";
        }
    }

    private void ReInsertData()
    {
        //txtCategoryPageID.Value = "";

        txtCacheTime.Text = "0";
        txtCategoryPageAbstract.Text = "";
        txtCategoryPageContent.Content = "";
        txtCategoryPageIcon.Text = "";
        txtCategoryPageImage.Text = "";
        txtCategoryPageName.Text = "";
        txtCategoryPageTitle.Text = "";
        txtMetaDescription.Text = ""; ;
        txtMetaKeyword.Text = "";
        txtViewOrder.Text = "0";
        chkIsAllowDelete.Checked = false;
        chkIsCache.Checked = false;
        chkIsHot.Checked = false;
        chkIsNew.Checked = false;
        chkIsTypical.Checked = false;
        chkIsVisible.Checked = false;

        General.SetSelectedItemInComboBox(null, cboCategoryPage);
    }

    #region Cac Ham Khac

    
    #endregion
}
